Handle wide power-of-two divisors in exact division#1271
Open
tob-joe wants to merge 1 commit into
Open
Conversation
Avoid indexing past the dividend when checking whether it has at least as many trailing zeros as a wider divisor. Use an unbounded right shift after the trailing-zero exactness check so shifting by the full dividend precision produces zero instead of panicking. Add a regression test for exact division by a wider power-of-two divisor. Co-authored-by: GPT 5.5 <gpt-5.5@openai.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1271 +/- ##
=======================================
Coverage 90.99% 91.00%
=======================================
Files 189 189
Lines 22139 22141 +2
=======================================
+ Hits 20146 20150 +4
+ Misses 1993 1991 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UintRef::div_exactcould panic when the divisor was wider than the dividend and had exactly as many trailing zeros as the dividend precision. In that case, exact division should report inexact division rather than indexing or shifting past the dividend.Fix
ensure_trailing_zerosnow handles zero-count requests that cover the full dividend width by checking whether the whole dividend is zero, instead of indexing the limb atnlimbs.After the trailing-zero exactness check,
div_exactnow uses an unbounded right shift so shifting by the full dividend precision produces zero instead of panicking.Tests
Added a regression test covering exact division of
U128::ONEby aU256power-of-two divisor at bit positionU128::BITS.Verified with:
This work was completed by Trail of Bits as part of the Patch The Planet project in collaboration with OpenAI. The issue was identified primarily by the Codex coding agent, and manually reviewed before submission.